#include "GameWindow.h"



GameWindow::GameWindow() : Window(780, 520, false){


    setCaption(L"Map Editor Alpha 0.0");

	std::wstring filename = Gosu::sharedResourcePrefix() + L"cursor.bmp";
	cursor.reset(new Gosu::Image(graphics(), filename));

	basicCFG = CfgReader::readConfig("basics.cfg");
	bigTiles = CfgReader::mapToBigTile(CfgReader::readConfig("../../data/bigTiles.cfg"));
	smallTiles = CfgReader::mapToSmallTile(CfgReader::readConfig("../../data/smallTiles.cfg"));
	

	map = new GameMap(graphics(), bigTiles, smallTiles, 
		CfgReader::strToInt(basicCFG["width"]),
		CfgReader::strToInt(basicCFG["height"]));

	if(CfgReader::strToInt(basicCFG["load"]) == 1)
		map->loadMap(basicCFG["map"].c_str());

	
	
	bigMenu = new TileMenu<bigTile>(graphics(),682,10,4,10,bigTiles, 22);
	smallMenu = new TileMenu<smallTile>(graphics(),682,100,2,20,smallTiles, 44);



}

void GameWindow::update(){

	double x = input().mouseX();
	double y = input().mouseY();

	if (input().down(Gosu::msLeft)){

		if(map->onMap(x,y))
			map->set(x,y);
		if(bigMenu->onMenu(x,y))
			map->defineBig(bigMenu->getTile(x,y));
		if(smallMenu->onMenu(x,y))
			map->defineSmall(smallMenu->getTile(x,y));
	}

}


void GameWindow::draw(){
    cursor->draw(input().mouseX(),input().mouseY(),100000);

	map->draw();
	bigMenu->draw();
	smallMenu->draw();

}



    
void GameWindow::buttonDown(Gosu::Button btn){
	if (btn == Gosu::kbEscape){

	   map->saveMap(basicCFG["map"].c_str());
       close();
	}
}
